package org.droid.campusorder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.droid.R;
import org.droid.campusorder.adapter.OrderListViewAdapter;
import org.droid.campusorder.bean.DishesBean;
import org.droid.campusorder.common.CommonMethod;
import org.droid.campusorder.common.GlobalData;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class OrderActivity extends MyActivity implements CommonMethod {
	public static TextView tv_sumprice;
	private TextView tv_telnum;
	private ListView lv_dishes;
	private Button btn_call;
	private Button btn_sms;
	private OrderListViewAdapter mAdapter;
	private ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();
	private float sumPrice = 0; // 总价
	private Dialog smsDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order);
		findAllViewById();
		setList();
		mAdapter = new OrderListViewAdapter(getApplicationContext());
		mAdapter.setList(mList);
		lv_dishes.setAdapter(mAdapter);
		setListenner();
		for (int i = 0; i < mList.size(); i++) {
			String tempPrice = mList.get(i).get("dishprice");
			String tempCount = mList.get(i).get("countnum");
			sumPrice += Float.parseFloat(tempPrice)
					* Integer.parseInt(tempCount);
		}
		tv_sumprice.setText(String.valueOf(sumPrice));
		tv_telnum.setText(getTelNumber());
		setSmsDialog();
	}

	/** 向List中填充数据 */
	private void setList() {
		List<DishesBean> dishesList = GlobalData.orderList;
		int length = dishesList.size();
		for (int i = 0; i < length; i++) {
			HashMap<String, String> hashMap = new HashMap<String, String>();
			String dishName = dishesList.get(i).getName();
			hashMap.put("dishname", dishName );
			String dishPrice = dishesList.get(i).getPrice();
			hashMap.put("dishprice", dishPrice);
			hashMap.put("countnum", "1");
			mList.add(hashMap);
		}
	}

	@Override
	public void setListenner() {
		btn_call.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("tel:" + getTelNumber());
				Intent intent = new Intent(Intent.ACTION_DIAL, uri);
				// 开始呼叫
				startActivity(intent);
			}
		});

		btn_sms.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				smsDialog.show();
			}
		});
	}

	@Override
	public void findAllViewById() {
		tv_sumprice = (TextView) findViewById(R.id.tv_order_sumprice);
		tv_telnum = (TextView) findViewById(R.id.tv_order_telnum);
		lv_dishes = (ListView) findViewById(R.id.lv_dish);
		btn_call = (Button) findViewById(R.id.btn_order_call);
		btn_sms = (Button) findViewById(R.id.btn_order_sms);
	}

	public static Handler calculateHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			tv_sumprice.setText(String.valueOf(msg.obj));
		}
	};

	// 设置短信对话框
	private void setSmsDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
		View dialogView = mInflater.inflate(R.layout.sms, null);
		Button btn_cancel = (Button) dialogView
				.findViewById(R.id.btn_sms_cancel);
		Button btn_confirm = (Button) dialogView
				.findViewById(R.id.btn_sms_confirm);
		final EditText et_remark = (EditText) dialogView
				.findViewById(R.id.et_sms_remark);
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				smsDialog.dismiss();
				et_remark.setText("");
			}
		});

		btn_confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Uri smsToUri = Uri.parse("smsto:" + getTelNumber());
				Intent intent = new Intent(Intent.ACTION_VIEW, smsToUri);
				String smsbody = getAdapterFianlData()
						+ et_remark.getText().toString().trim();
				intent.putExtra("sms_body", smsbody);
				startActivity(intent);
				smsDialog.dismiss();
				et_remark.setText("");
			}
		});
		builder.setView(dialogView);
		smsDialog = builder.create();
	}

	/** 获取listview中的菜单详情 */
	private String getAdapterFianlData() {
		String tempCanche = "";
		int length = mAdapter.getCount();
		for (int i = 0; i < length; i++) {
			if (Integer.parseInt(mAdapter.countTextViewsCanche[i].getText()
					.toString().trim()) != 0) {
				tempCanche += mAdapter.dishnameTextViewsCanche[i].getText()
						.toString().trim()
						+ mAdapter.countTextViewsCanche[i].getText().toString()
								.trim() + "份" + ".";
			}
		}
		return tempCanche;
	}

	/** 获取电话号码 */
	private String getTelNumber() {
		return "10086";
	}

}
